#-------------------------------------------------------------------
# Project Invasion of the aliens 
# name game.py
# purpose to kill enamys 
#
# authors Chris L'Heureux, Tyler Austin Plante, Jackson Morse
# main author Tyler Austin Plante
#
# created 12/3/12
# Copyright (C) Jackson Morse
# License New BSD\
#------------------------------------------------------------------  
import pygame, math, sys, random
from Player import Player

class Bullet():
    def __init__(self, player, screenSize):
        self.surfaces = []
        self.surfaces += [pygame.image.load("images/Bullet/bullet.png")]
        self.frame = 0
        self.maxFrame = len(self.surfaces)-1
        self.surface = self.surfaces[self.frame]
        self.rect = self.surface.get_rect() 
        self.baseRect = self.rect
        self.rect = self.surface.get_rect()
        self.speed = 10
        self.x = player.x
        self.y = player.y
        self.angle = player.angle
        self.screenWidth = screenSize[0]
        self.screenHeight = screenSize[1]
        self.radius = (self.rect.width/2 + self.rect.height/2)/2
        self.living = False
        self.place = player.rect.center 
        
    def  __str__(self):
        return ("I'm the BULLET and >>>>>I<<<<< am KILLING " + str(self.rect.center) + str(self.speed) + str(self.living))
 
    def place(self, position):
        self.rect.center = position
        
    def die(self):
        self.living = False
    
    def newPos(self):
        self.angle = self.angle % 360
        if self.angle < 0:
            self.angle = 360 - math.abs(self.angle)
        if self.angle == 0:
            self.x += self.speed
            self.y += 0
        elif 0 < self.angle < 90: 
            self.x += self.speed * math.cos(math.radians(self.angle))
            self.y += -self.speed * math.sin(math.radians(self.angle))
        elif self.angle == 90:
            self.x += 0
            self.y += -self.speed
        elif 90 < self.angle < 180:
            self.x += -self.speed * math.sin(math.radians(self.angle-90))
            self.y += -self.speed * math.cos(math.radians(self.angle-90))
        elif self.angle == 180:
            self.x += -self.speed
            self.y += 0
        elif 180 < self.angle < 270:
            self.x += -self.speed * math.cos(math.radians(self.angle-180))
            self.y += self.speed * math.sin(math.radians(self.angle-180))
        elif self.angle == 270:
            self.x += 0
            self.y += self.speed 
        elif 270 < self.angle < 360:
            self.x += self.speed * math.sin(math.radians(self.angle-270))
            self.y += self.speed * math.cos(math.radians(self.angle-270))
 
    def move (self):
        self.newPos()
        self.rect.center = [int(self.x), int(self.y)]
    
    def collideWall(self, screenWidth, screenHeight):
        if (self.rect.left < 0 or self.rect.right > screenWidth):
                self.living = False 
        if (self.rect.top < 0 or self.rect.top > screenHeight):
                self.living = False
            
            
    def distToPoint(self, pt):
        x1 = self.rect.center[0]
        x2 = pt[0]
        y1 = self.rect.center[1]
        y2 = pt[1]
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))            

    
    def collideEnemy(self, other):
         if (self.rect.right > other.rect.left 
             and self.rect.left < other.rect.right):
             if (self.rect.bottom > other.rect.top and 
                 self.rect.top < other.rect.bottom):
                 if (self.distToPoint(other.rect.center)
                     < self.radius + other.radius):                 
                     other.die()
                     self.die()
                     
    def collidePlayer(self, other):
         if (self.rect.right > other.rect.left 
             and self.rect.left < other.rect.right):
             if (self.rect.bottom > other.rect.top and 
                 self.rect.top < other.rect.bottom):
                 if (self.distToPoint(other.rect.center)
                     < self.radius + other.radius):                 
                     other.die()
                     self.die()     
                        
    
    
        
        
                     